#!/usr/bin/python

"""Project Euler Solution 012

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
import re

replacements = [
    (u'IIII', u'IV'),
    (u'VIV', u'IX'),
    (u'VV', u'X'),
    (u'XXXX', u'XL'),
    (u'LXL', u'XC'),
    (u'LL', u'C'),
    (u'CCCC', u'CD'),
    (u'DCD', u'CM'),
    (u'DD', u'M'),
]

def apply_replacements(roman_numeral_string):
    result = roman_numeral_string

    for replacement in replacements:
        result = result.replace(replacement[0], replacement[1])

    return result

def get_answer():
    """Question:
    
    The rules for writing Roman numerals allow for many ways of writing each 
    number (see About Roman Numerals...). However, there is always a "best" 
    way of writing a particular number.

    For example, the following represent all of the legitimate ways of writing 
    the number sixteen:

    IIIIIIIIIIIIIIII
    VIIIIIIIIIII
    VVIIIIII
    XIIIIII
    VVVI    
    XVI

    The last example being considered the most efficient, as it uses the least 
    number of numerals.

    The 11K text file, roman.txt (right click and 'Save Link/Target As...'), 
    contains one thousand numbers written in valid, but not necessarily minimal, 
    Roman numerals; that is, they are arranged in descending units and obey the 
    subtractive pair rule (see About Roman Numerals... for the definitive rules for this problem).

    Find the number of characters saved by writing each of these in their minimal form.

    Note: You can assume that all the Roman numerals in the file contain no more than four consecutive 
        identical units.
    """
    
    total_saved = 0

    with open('../data/089.txt', 'r') as roman_numeral_file:
        for unstripped_roman_numeral_string in roman_numeral_file:
            roman_numeral_string = unstripped_roman_numeral_string.strip()

            total_saved += len(roman_numeral_string) - len(apply_replacements(roman_numeral_string))

        
    return total_saved

if __name__ == "__main__":
    cProfile.run("print(get_answer())")
